package com.zhongwang.mms.module.jiaGongSemi.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.jiaGongSemi.dao.AccountQueryDao;
import com.zhongwang.mms.module.jiaGongSemi.dao.StockQueryDao;
import com.zhongwang.mms.module.jiaGongSemi.model.JSemiDetail;
import com.zhongwang.mms.module.jiaGongSemi.model.JSemiInventory;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ExportExcelUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName： AllocationManageService
 * @Description： 库存查询业务逻辑层
 * @Author： China.zh
 * @Date： 2020/4/6 13:41
 * @Version： V1.0
 **/
@Log4j2
@Service
public class AccountQueryService {


    @Autowired
    private LogService logService;
    @Value("${user.upload.path}")
    private String uploadPath;
    @Autowired
    private StockQueryDao stockQueryDao;
    @Value("${spring.profiles.active}")
    private String active;
    @Autowired
    private AccountQueryDao accountQueryDao;


    public List<JSemiDetail> findAll(JSemiDetail jSemiDetail) {
        return accountQueryDao.findAllByQuery(jSemiDetail);
    }

    /**
     * 导出
     *
     * @param param
     */
    public JsonResult exportExcel(JSemiDetail param) {
        JsonResult jsonResult = new JsonResult();
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        logService.saveLog("甲供件-毛坯与铝料出入库综合查询——导出", "用户:【" + user.getName() + "】执行导出操作！");
        //存储/读取Excel的文件夹的路径 mms文件夹下/用户账户/
        String path = uploadPath + user.getAccount() + "/";
        String excelFileName = "甲供件-毛坯与铝料出入库综合查询.xlsx";
        try {
            //1.创建临时内存表名
            String tempTableName = "##" + user.getAccount() + (UUID.randomUUID().toString().replace("-", ""));
            param.setTempTableName(tempTableName);
            Subject userSubject = ShiroUtils.getSubject();
            //2.查询出数据导入到临时表
            accountQueryDao.createAllExportByQueryTempTableNotMoney(param);
            //3.导出Excel
            if (ExportExcelUtils.BuildExcel(path, excelFileName, tempTableName, active, true)) {
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setData(excelFileName);
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
            }
        } catch (Exception e) {
            log.debug(e.getMessage());
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }

    public JSemiDetail findAllById(Integer odrId) {
        return  accountQueryDao.findAllById(odrId);
    }
    /**
     * 查询成品入库
     *
     * @param jSemiDetail
     * @return
     */
    public List<JSemiDetail> getInFinish(JSemiDetail jSemiDetail) {
        return accountQueryDao.selectDetailByOdrId(jSemiDetail.getOdrId());
    }
}
